package com.bupt.liutong.admin.sch.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.sch.bo.TeacherBo;
import com.bupt.liutong.admin.sch.bo.TeacherPhotoBo;
import com.bupt.liutong.admin.sch.form.TeacherForm;
import com.bupt.liutong.util.BeanUtils;

public class TeacherAction extends BaseAction {

	private TeacherBo teacherBo;
	private TeacherPhotoBo teacherPhotoBo;
	
	public ActionForward getTeachersByPage(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		TeacherForm teacherForm = (TeacherForm) form;
		request.setAttribute("teacherFormList", teacherBo.getTeachersByPage(
				teacherForm, request));
		putPageInfo(request, teacherForm);
		return mapping.findForward("getTeachersByPageSuccess");
	}

	public ActionForward insertTeacher(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("insertTeacherSuccess");
	}

	public ActionForward insertTeacherDo(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		TeacherForm teacherForm = (TeacherForm) form;
		teacherBo.insertTeacher(teacherForm, request);
		return mapping.findForward("insertTeacherDoSuccess");
	}

	public ActionForward updateTeacher(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TeacherForm teacherForm = (TeacherForm) form;
		BeanUtils.copyProperties(teacherForm, teacherBo.getTeacherById(teacherForm));
		request.setAttribute("photoFormList", 
				teacherPhotoBo.getTeacherPhotos(teacherForm));
		return mapping.findForward("updateTeacherSuccess");
	}

	public ActionForward updateTeacherDo(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		TeacherForm teacherForm = (TeacherForm) form;
		teacherBo.updateTeacher(teacherForm, request);
		return mapping.findForward("updateTeacherDoSuccess");
	}

	public ActionForward deleteTeachers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		teacherBo.deleteTeachers(ids);
		return mapping.findForward("deleteTeachersSuccess");
	}

	public void setTeacherBo(TeacherBo teacherBo) {
		this.teacherBo = teacherBo;
	}
	
	public void setTeacherPhotoBo(TeacherPhotoBo teacherPhotoBo) {
		this.teacherPhotoBo = teacherPhotoBo;
	}
}
